//
//  main.m
//  sad
//
//  Created by mac on 16-5-17.
//  Copyright (c) 2016年 31231. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        // insert code here...
        
        NSString *path=@"/Users/mac/Desktop/00001.vcf";
        NSError *err;
        NSString *str=[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        NSArray *arr=[str componentsSeparatedByString:@"\n"];
        NSMutableArray *res=[NSMutableArray new];
        NSMutableDictionary *dct;
        if (err!=nil) {
            NSLog(@"sasd");
        }
        NSLog(@"%@",str);
        for(NSString *line in arr){
            if ([line hasPrefix:@"BEGIN"]) {
                NSLog(@"这条记录已开始");
                dct=[NSMutableDictionary new];
            }
            if ([line hasPrefix:@"END"]) {
                NSLog(@"这条记录已结速");
                [res addObject:dct];
            }
            if ([line hasPrefix:@"TEL"]) {
                NSLog(@"找到一条电话记录");
                NSArray *a=[line componentsSeparatedByString:@";"];
                NSString *last=[a lastObject];
                last=[last substringFromIndex:5];
                NSLog(@"a:%@",last);
                dct[@"tel"]=last;
            }
            if ([line hasPrefix:@"FN"]) {
                NSLog(@"找到一条FN 也就是姓名");
                NSArray *name=[ line componentsSeparatedByString:@":"];
                NSString *names=[name lastObject];
                names=[names stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                names=names.stringByRemovingPercentEncoding;
                NSLog(@"names%@",names);
                dct[@"names"]=names;
            }
        }
        NSLog(@"%@",res);
        [res writeToFile:@"/Users/mac/Desktop/piaoliang/res.txt" atomically:NO];
        NSData *data=[NSJSONSerialization dataWithJSONObject:res options:NSJSONWritingPrettyPrinted error:nil];
        [data writeToFile:@"/Users/mac/Desktop/piaoliang/res2.txt" atomically:NO];
        NSString *strt=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",strt);
        NSMutableArray *res2=[NSMutableArray new];
        for(NSDictionary *dict in res){
            Person *p=[Person new];
            [p setValuesForKeysWithDictionary:dict];
            [res2 addObject:p];
        }
        NSLog(@"%@",res2);
    }
    return 0;
}





